package com.nwpu.coursesupport.controller;

import com.alibaba.fastjson.JSON;
import com.nwpu.coursesupport.criteria.TaskSearchCriteria;
import com.nwpu.coursesupport.entity.bean.JSONResult;
import com.nwpu.coursesupport.entity.bean.Response;
import com.nwpu.coursesupport.service.imple.TaskService;
import com.nwpu.coursesupport.service.param.TaskServiceParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/task")
public class TaskController extends AbstractController {

    @Autowired
    private TaskService taskService;

    @RequestMapping("/detail")
    public String detail(@RequestParam("id") int id, HttpServletRequest request) {
        JSONResult result = invokeService("task", "detail", "{\"id\":\"" + id + "\"}");

        request.setAttribute("data", this.parseData(result));
        return "/teapage/courseTaskEdit";
    }

    @RequestMapping("/list")
    public String list(@RequestParam("courseId") Integer courseId, Integer pageNo, HttpServletRequest request) {
        JSONResult rst = new JSONResult();
        rst.fail();
        TaskSearchCriteria sc = new TaskSearchCriteria();
        sc.setCourseId(courseId);
        sc.setPageNo(pageNo == null ? 1 : pageNo);
        JSONResult result = taskService.listData(sc);
        request.setAttribute("data", this.parseData(result));
        return "/teapage/courseTaskIndex";
    }

    @RequestMapping("/ajaxDetail")
    @ResponseBody
    public JSONResult ajaxDetail(@RequestParam("id") int id) {
        JSONResult result = invokeService("task", "detail", "{\"id\":\"" + id + "\"}");
        return result;
    }

    @RequestMapping("/ajaxListData")
    @ResponseBody
    public JSONResult ajaxListData(@RequestParam("data") String data) {
        JSONResult result = invokeService("task", "listData", data);
        return result;
    }


    @RequestMapping("/ajaxCreate")
    @ResponseBody
    public JSONResult ajaxCreate(@RequestParam("data") String data) throws Exception {
        System.out.println("data===:" + data);
//        TaskServiceParam.CreateParam param = JSONUtils.json2pojo(data, TaskServiceParam.CreateParam.class);

        TaskServiceParam.CreateParam param = JSON.parseObject(data, TaskServiceParam.CreateParam.class);
//        System.out.println("======" + param);
        //        JSONResult result = invokeService("task", "create", data);
//        return result;
        return taskService.create1(param);
//        return null;
    }

    @RequestMapping("/ajaxCreateLessonTask")
    @ResponseBody
    public JSONResult ajaxCreateLessonTask(@RequestParam("data") String data) {
        JSONResult result = invokeService("task", "createLessonTask", data);
        return result;
    }

    @RequestMapping("/ajaxUpdate")
    @ResponseBody
    public JSONResult ajaxUpdate(@RequestParam("data") String data) throws Exception {
        System.out.println("======"+data);
        TaskServiceParam.CreateParam param = JSON.parseObject(data, TaskServiceParam.CreateParam.class);

        System.out.println("-----"+param);
//        JSONResult result = invokeService("task", "update", data);
        return  taskService.update1(param);
//        return result;r
//     null;
    }

    @RequestMapping("/ajaxDelete")
    @ResponseBody
    public JSONResult ajaxDelete(@RequestParam("id") int id) {
        JSONResult result = invokeService("task", "remove", "{\"id\":\"" + id + "\"}");
        return result;
    }

    @RequestMapping("/ajaxDeleteAll")
    @ResponseBody
    public JSONResult ajaxDeleteAll(@RequestParam("data") String data) {
        JSONResult result = invokeService("task", "removeAll", data);
        return result;
    }

    @RequestMapping("/ajaxDeleteAttachment")
    @ResponseBody
    public JSONResult ajaxDeleteAttachment(@RequestParam("data") String data) {
        JSONResult result = invokeService("task", "deleteAttachment", data);
        return result;
    }

    @RequestMapping("/ajaxAddAttachment")
    @ResponseBody
    public JSONResult ajaxAddAttachment(@RequestParam("data") String data) {
        JSONResult result = invokeService("task", "addAttachment", data);
        return result;
    }

    @ResponseBody
    @RequestMapping("/ajaxGetTask/{taskId}")
    public Response ajaxGetTask(@PathVariable Integer taskId){
        return taskService.getTaskById(taskId);
    }

}
